package my.util;

/**
 * <p>
 *
 * </p>
 *
 * @author AJun
 * @since 2020/8/12
 */
public abstract class AbstractList<E> implements List<E> {

    /**
     * List大小
     */
    protected int size = 0;

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public boolean contains(E element) {
        return indexOf(element) >= 0;
    }

    public void add(E element) {
        add(size, element);
    }

    protected void rangeCheck(int index) {
        if (index >= size) // 如果小于 0，由数组本身抛出 IndexOutOfBoundsException
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    protected void rangeCheckForAdd(int index) {
        if (index > size || index < 0)
            throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
    }

    protected String outOfBoundsMsg(int index) {
        return "Index: " + index + ", Size: " + size;
    }

}
